package com.mes.loco.aps.server.controller.oms;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.MyHelperService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.oms.OMSCheckMsg;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PengYouWang
 * 2023-3-16 11:19:43
 */
@RestController
@RequestMapping("/api/OMSCheckMsg")
public class OMSCheckMsgController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(OMSCheckMsgController.class);

    final
    MyHelperService wOMSService;

    public OMSCheckMsgController(MyHelperService wOMSService) {
        this.wOMSService = wOMSService;
    }

    /**
     * 条件查询
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wType = StringUtils.parseInt(request.getParameter("Type"));
            int wPlanTaskID = StringUtils.parseInt(request.getParameter("PlanTaskID"));

            ServiceResult<List<OMSCheckMsg>> wServiceResult = wOMSService.OMS_QueryCheckMsgList(wLoginUser, wType,
                    wPlanTaskID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "!!!");
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            OMSCheckMsg wOMSCheckMsg = CloneTool.Clone(wParam.get("data"), OMSCheckMsg.class);
            if (wOMSCheckMsg == null)
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_UpdateCheckMsg(wLoginUser, wOMSCheckMsg);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 保存检查结果集
     */
    @PostMapping("/UpdateList")
    public Object SaveTaskItemList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<OMSCheckMsg> wOMSCheckMsgList = CloneTool.CloneArray(wParam.get("data"), OMSCheckMsg.class);
            if (wOMSCheckMsgList == null || wOMSCheckMsgList.size() <= 0 || wOMSCheckMsgList.stream().anyMatch(p -> p.PlanTaskID <= 0)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wOMSService.OMS_UpdateCheckMsgList(wLoginUser, wOMSCheckMsgList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
